package com.gy.paidao.util;


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.provider.MediaStore;

public class PhotoUtil {
	/**
	 * 锟叫讹拷图片锟斤拷式
	 *
	 * @param path
	 * @return
	 */
	public static boolean isImageFormat(String path) {

		if (path.endsWith("png") || path.endsWith("jpg")
				|| path.endsWith("bmp") || path.endsWith("jpeg")) {

			return true;
		}
		return false;
	}
	/**
	 * 锟斤拷锟斤拷锟斤拷锟�
	 *
	 * @author zts edit at 2013-01-17
	 */
	public static Intent getCameraIntent(File tempFile) {
		Uri imageFile = Uri.fromFile(tempFile);
		Intent cameraIntent = new Intent(
				MediaStore.ACTION_IMAGE_CAPTURE);
		cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageFile);
		return cameraIntent;
	}

	/**
	 * 锟斤拷锟斤拷图片锟介看锟斤拷选锟斤拷图片
	 *
	 * @author zts edit at 2013-01-17
	 */
	public static Intent getPictrueIntent() {
		try {
			Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
			intent.setType("image/*");
			intent.addCategory(Intent.CATEGORY_OPENABLE);
			return intent;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	// ------锟斤拷锟斤拷锟酵计�锟斤拷压锟斤拷锟斤拷锟斤拷----------------------------------
	/**
	 * 锟斤拷莩锟斤拷锟饺的达拷小锟斤拷bitmap锟斤拷锟斤拷锟斤拷锟脚ｏ拷filePath 锟斤拷图片锟斤拷路锟斤拷
	 *
	 */
	public static Bitmap getSizedBitmap(int width, int height, String filePath) {
		if (null != filePath && !filePath.equals("")) {
			BitmapFactory.Options opts = null;
			if (width > 0 && height > 0) {
				opts = new BitmapFactory.Options();
				opts.inJustDecodeBounds = true;
				BitmapFactory.decodeFile(filePath, opts);
				// 锟斤拷锟斤拷图片锟斤拷锟脚憋拷锟斤拷
				final int minSideLength = Math.min(width, height);
				opts.inSampleSize = computeSampleSize(opts, minSideLength,
						width * height);
				opts.inJustDecodeBounds = false;
				opts.inInputShareable = true;
				opts.inPurgeable = true;
				opts.inPreferredConfig = Bitmap.Config.ARGB_8888;

			}
			try {

				// System.out.println("------锟斤拷锟斤拷-------"+(bitmap2byte(BitmapFactory.decodeFile(filePath,
				// opts))).length);
				return BitmapFactory.decodeFile(filePath, opts);
			} catch (OutOfMemoryError e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public static Bitmap handlerBitmap(String filePath,int degree) {
		if (null != filePath && !filePath.equals("")) {
			BitmapFactory.Options opts = null;
			Bitmap bitmap = BitmapFactory.decodeFile(filePath);
			if(bitmap == null)
				return null;
			int width = bitmap.getWidth();
			int height = bitmap.getHeight();
			if(width < 800){
				return bitmap;
			} else {

				Matrix matrix = new Matrix();
				matrix.postRotate(degree);
				Bitmap bitmapa = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
						bitmap.getHeight(), matrix, true);

				// 计算缩放比例
				float scaleWidth = ((float) 800) / bitmapa.getWidth();
				// 取得想要缩放的matrix参数
				matrix = new Matrix();
				matrix.postScale(scaleWidth, scaleWidth);
				Bitmap newbm = Bitmap.createBitmap(bitmapa, 0, 0, bitmapa.getWidth(), bitmapa.getHeight(), matrix, true);
				return newbm;
			}
		}
		return null;

	}

	public static int computeSampleSize(BitmapFactory.Options options,
										int minSideLength, int maxNumOfPixels) {
		int initialSize = computeInitialSampleSize(options, minSideLength,
				maxNumOfPixels);

		int roundedSize;
		if (initialSize <= 8) {
			roundedSize = 1;
			while (roundedSize < initialSize) {
				roundedSize <<= 1;
			}
		} else {
			roundedSize = (initialSize + 7) / 8 * 8;
		}

		return roundedSize;
	}
	private static int computeInitialSampleSize(BitmapFactory.Options options,
												int minSideLength, int maxNumOfPixels) {
		double w = options.outWidth;
		double h = options.outHeight;

		int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
				.sqrt(w * h / maxNumOfPixels));
		int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
				Math.floor(w / minSideLength), Math.floor(h / minSideLength));

		if (upperBound < lowerBound) {
			// return the larger one when there is no overlapping zone.
			return lowerBound;
		}

		if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
			return 1;
		} else if (minSideLength == -1) {
			return lowerBound;
		} else {
			return upperBound;
		}
	}
	public static Bitmap getSizedBitmap(int mWidth, int mHeight, InputStream is)
			throws Exception {
		byte[] bt = getBytes(is);
		return getSizedBitmap(mWidth, mHeight, bt);
	}
	private static byte[] getBytes(InputStream is) throws Exception {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] b = new byte[1024];
		int len = 0;

		while ((len = is.read(b)) != -1) {
			baos.write(b, 0, len);
		}
		byte[] bytes = baos.toByteArray();
		baos.close();
		is.close();
		return bytes;
	}


	public static Bitmap getSizedBitmap(int mWidth, int mHeight, byte[] bytes) {
		BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
		bmpFactoryOptions.inJustDecodeBounds = true;
		Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length,
				bmpFactoryOptions);
		// Bitmap bmp = BitmapFactory.decodeStream(is, null, bmpFactoryOptions);
		float heightRatio = (float) Math.ceil(bmpFactoryOptions.outHeight
				/ mHeight);
		float widthRatio = (float) (Math.ceil(bmpFactoryOptions.outWidth
				/ mWidth));
		// 锟叫讹拷锟角凤拷要锟斤拷锟斤拷锟斤拷锟斤拷
		if (heightRatio > 1 || widthRatio > 1) {
			if (heightRatio > widthRatio) {
				// 锟竭度变化锟斤拷,锟斤拷锟竭讹拷锟斤拷锟斤拷
				bmpFactoryOptions.inSampleSize = (int) heightRatio;
			} else {
				// 锟斤拷缺浠�锟斤拷,锟斤拷锟斤拷锟斤拷锟斤拷锟�
				bmpFactoryOptions.inSampleSize = (int) widthRatio;
			}
		}
		bmpFactoryOptions.inJustDecodeBounds = false;
		bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length,
				bmpFactoryOptions);
		return bmp;
	}
}
